<?php
	ini_set("memory_limit","256M");
	class UserAction extends CommonAction{
		//兑奖
		public function doExchangeNew(){
			$uid = isset($_GET['uid']) && $_GET['uid'] != '' ? $_GET['uid'] : 0;
			if($uid != 0){
				$User = M('User');
				$User->is_exchange = 1;
				$rzt = $User->where('id='.$uid)->save();
				if($rzt > 0){
					$this->success('兑换成功','../../userInfo');
				}else{
					$this->error('兑换失败');
				}
			}
		}

		
		public function topNInfo(){
			
			$Score = D('Score');
			$queryResult = $Score->relation('User')->select();
			//var_dump($queryResult);exit;
		
			if($queryResult!=null){
				import('ORG.Util.Page');// 导入分页类
				$count      = count($queryResult);// 查询满足要求的总记录数
				$Page       = new Page($count,60);// 实例化分页类 传入总记录数和每页显示的记录数
				$show       = $Page->show();// 分页显示输出
				// 进行分页数据查询 注意limit方法的参数要使用Page类的属性				
				$list = $Score->relation('User')->order('score desc')->limit($Page->firstRow.','.$Page->listRows)->select();
				//var_dump($list);exit;
				$this->assign('data',$list);// 赋值数据集
				$this->assign('page',$show);// 赋值分页输出		
			}
		
			unset($queryResult);
			$this->display();
		
		}

		
		public function searchTopN(){
		
			$where=" where 1=1";
		
			if(!empty($_GET['startdate'])){
					
				$where.=" and DATE_FORMAT( s.joindate,  '%Y-%m-%d' ) >='".date("Y-m-d",strtotime($_GET['startdate']))."'";
			}
		
			if(!empty($_GET['enddate'])){
				$where.=" and DATE_FORMAT( s.joindate,  '%Y-%m-%d' ) <='".date("Y-m-d",strtotime($_GET['enddate']))."'";
			}
		
			if(!empty($_GET['username'])){
				$where.=" and u.username like '%".$_GET['username']."%'";
			}
		
			if(!empty($_GET['userphone'])){
				$where.=" and u.userphone like '%".$_GET['userphone']."%'";
			}
		
			$queryStr = "select u.id as uid,u.username as username,u.userphone as userphone,s.score as score,u.regtime,s.joindate
						from tp_user u join tp_score s on u.id=s.uid".$where;
		
		
			$Model = new Model(); // 实例化一个model对象 没有对应任何数据表
		
			$queryResult = $Model->query($queryStr);
		
		
			if($queryResult!=null){
		
				//带入搜索参数
				$parameter = 'userphone='.urlencode($_GET['userphone']).'&startdate='.urlencode($_GET['startdate']).'&enddate='.urlencode($_GET['enddate']).'&username='.urlencode($_GET['username']);
		
				import('ORG.Util.Page');// 导入分页类
				$count      = count($queryResult);// 查询满足要求的总记录数
				$Page       = new Page($count,60,$parameter);// 实例化分页类 传入总记录数和每页显示的记录数
				$show       = $Page->show();// 分页显示输出
				// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
				$where.=" order by s.score desc limit ".$Page->firstRow.",".$Page->listRows;
				$queryStr1 = "select u.id as uid,u.username as username,u.userphone as userphone,s.score as score,u.regtime,s.joindate
						from tp_user u join tp_score s on u.id=s.uid".$where;
		
				$list = $Model->query($queryStr1);
				$this->assign('data',$list);// 赋值数据集
				$this->assign('page',$show);// 赋值分页输出
		
			}
		
			unset($queryResult);
			$this->display('topNInfo'); // 输出模板
		}
		
		/**
		 *
		 * 导出 exportTopNData Excel
		 */
		function exportTopNData(){//导出Excel
		
			$xlsName  = "成绩排行表";
			$xlsCell  = array(
					array('index','序号')	,
					array('score','成绩'),
					array('joindate','参与时间'),
					array('username','用户姓名'),
					array('userphone','用户电话'),					
		
			);
			
			$xlsModel = new Model();
			
			$where=" where 1=1";
			
			if($_GET['startdate']!='blank'){
					
				$where.=" and DATE_FORMAT( s.joindate,  '%Y-%m-%d' ) >='".date("Y-m-d",strtotime($_GET['startdate']))."'";
			}
			
			if($_GET['enddate']!='blank'){
				$where.=" and DATE_FORMAT( s.joindate,  '%Y-%m-%d' ) <='".date("Y-m-d",strtotime($_GET['enddate']))."'";
			}
				
			if($_GET['username']!='blank'){
				$where.=" and u.username like '%".$_GET['username']."%'";
			}
				
			if($_GET['userphone']!='blank'){
				$where.=" and u.userphone like '%".$_GET['userphone']."%'";
			}
			
			$where.=" order by s.score desc ";
			$queryStr1 ="select u.id as uid,u.username as username,u.userphone as userphone,s.score as score,u.regtime,s.joindate
						from tp_user u join tp_score s on u.id=s.uid".$where;
			
			$xlsData = $xlsModel->query($queryStr1);
			
			
			$i=1;
			foreach ($xlsData as $k => $v)
			{
				$xlsData[$k]['index']=$i;
				$i++;
			}
				
			$this->exportExcel($xlsName,$xlsCell,$xlsData);
		
		}
		
		/**
		 * 报表统计：参与人数、分享次数
		 */
		public function reportInfo() {
			$shareCount = 0;
			$zhuliCount = 0;
			$userCount = 0;
			
			$Model = new Model(); // 实例化一个model对象 没有对应任何数据表
			
			$queryStr ="select c.value from tp_config as c where varname='cfg_share_count'";
			$queryResult = $Model->query($queryStr);
			$shareCount = $queryResult['0']['value'];
			
			//
			$User = M('User');
			$userCount = $User->count();
			
			$list = array(
						'shareCount' => $shareCount,
						'userCount' => $userCount,
					);
			
			$this->assign('data',$list);// 赋值数据集
			
			$this->display();
		}
		
		public function prizeInfo() {
				
			$User = M('User'); // 实例化User对象
			import('ORG.Util.Page');// 导入分页类
			$count      = $User->count();// 查询满足要求的总记录数
			$Page       = new Page($count,C('USER_PAGE_COUNT'));// 实例化分页类 传入总记录数和每页显示的记录数
			$show       = $Page->show();// 分页显示输出
			// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
			$list = $User->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
			$this->assign('data',$list);// 赋值数据集
			$this->assign('page',$show);// 赋值分页输出
			$this->display(); // 输出模板
		}
		
		/**
		 *
		 * 导出中奖名单 Excel
		 */
		function exportPrizeData(){//导出Excel
		
			$xlsName  = "中奖名单";
			$xlsCell  = array(
					array('index','序号'),
					array('username','用户名称'),
					array('userphone','用户手机'),
					array('giftname','奖品名称')
		
			);
			$xlsModel = M('User');
			
			$where = "giftname is not null and giftname != ''";
			if(isset($_GET['username']) && $_GET['username']!='blank'){
				$where .= " and username like '%".$_GET['username']."%'";
			}
			if(isset($_GET['userphone']) && $_GET['userphone']!='blank'){
				$where .= " and userphone like '%".$_GET['userphone']."%'";
			}
				
			$xlsData  =$xlsModel->where($where)->Field('username,userphone,giftname,regtime')->order('id desc')->select();
				
			$i=1;
			foreach ($xlsData as $k => $v)
			{
				$xlsData[$k]['index']=$i;
				$i++;
			}
				
			$this->exportExcel($xlsName,$xlsCell,$xlsData);
		
		}
		
		public function doPrize() {
			$usernum = $_POST['usernum'];
			$giftname = $_POST['giftname'];
			
			$User = M('User');
			// 合法性判断
			$count = $User->where("rys >= 5 and (giftname is null or giftname = '')")->count();
			if ($count < $usernum) {
				echo json_encode(array('success'=>'ng','msg'=>'抽奖人数大于完成助力人数！','count'=>$count));
				exit;
			}
			
			$sql = "select u.id from tp_user as u where u.rys >= 5 and (u.giftname is null or u.giftname = '') ORDER BY RAND() LIMIT ".$usernum;
			$list = $User->query($sql);
			if (count($list) > 0) {
				foreach ($list as $row) {
					$User->where("id=".$row['id'])->setField("giftname", $giftname);
				}
			}
			
			echo json_encode(array('success'=>'ok','msg'=>'抽奖成功！','count'=>$count));
			exit;
			
		}

		public function redPackList(){
			$Redpack = M('Redpack');
            import('ORG.Util.Page');// 导入分页类
            $count      = $Redpack->count();// 查询满足要求的总记录数
            $Page       = new Page($count,C('USER_PAGE_COUNT'));// 实例化分页类 传入总记录数和每页显示的记录数
            $show       = $Page->show();// 分页显示输出
            // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
            $list = $Redpack->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
            $this->assign('data',$list);// 赋值数据集
            $this->assign('page',$show);// 赋值分页输出
            $this->display(); // 输出模板
		}

		public function exportRedpackData(){
            $xlsName  = "红包数据表";
            $xlsCell  = array(
                array('index','序号'),
                array('id','红包ID'),
                array('wxid','用户WxOpenid'),
                array('score','得分'),
                array('amount','红包金额'),
                array('status','状态'),
                array('remark','备注'),
                array('created','创建时间'),
                array('updated','更新时间'),

            );


            $xlsModel = new Model();

            $where=" where 1=1";


            $where.=" order by id desc";
            $queryStr1 ="select id,wxid,score,amount,status,remark,created,updated from tp_redpack".$where;

            $xlsData = $xlsModel->query($queryStr1);
            //var_dump($queryStr1);exit;
            $i=1;
            foreach ($xlsData as $k => $v)
            {
                $xlsData[$k]['index']=$i;
                $i++;
                if($xlsData[$k]['status'] == 1){
                    $xlsData[$k]['status'] = '已发放';
                }else{
                    $xlsData[$k]['status'] = '未发放';
                }
            }

            $this->exportExcel($xlsName,$xlsCell,$xlsData);
		}
		
		public function userInfo(){			
			$User = M('User'); // 实例化User对象
			import('ORG.Util.Page');// 导入分页类
			$count      = $User->count();// 查询满足要求的总记录数
			$Page       = new Page($count,C('USER_PAGE_COUNT'));// 实例化分页类 传入总记录数和每页显示的记录数
			$show       = $Page->show();// 分页显示输出
			// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
			$list = $User->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
			$this->assign('data',$list);// 赋值数据集
			$this->assign('page',$show);// 赋值分页输出
			$this->display(); // 输出模板
		}
		
		public function searchUser(){
			$map=array();
			if(isset($_GET['username']) && $_GET['username'] != ''){
				
				$map['username'] = array('like','%'.$_GET['username'].'%');
			}
			if(!empty($_GET['userphone'])){
				$map['userphone'] = array('like','%'.$_GET['userphone'].'%');
			}
			if(isset($_GET['isexchange']) && $_GET['isexchange'] != ''){
				
				$map['is_exchange'] = $_GET['isexchange'];
			}
			//var_dump($map);exit;
			$parameter = 'username='.urlencode($_GET['username']).'&userphone='.urlencode($_GET['userphone']).'&realname='.urlencode($_GET['realname']).'&is_exchange='.urlencode($_GET['is_exchange']);
			
			$User = M('User'); // 实例化User对象
			import('ORG.Util.Page');// 导入分页类
			$count      = $User->where($map)->count();// 查询满足要求的总记录数
			$Page       = new Page($count,C('USER_PAGE_COUNT'),$parameter);// 实例化分页类 传入总记录数和每页显示的记录数
			$show       = $Page->show();// 分页显示输出
			// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
			$list = $User->where($map)->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
			$this->assign('data',$list);// 赋值数据集
			$this->assign('page',$show);// 赋值分页输出
			$this->display('userInfo'); // 输出模板
			
		}
		
		/**
		 *
		 * 导出 USER Excel
		 */
		function exportUserData(){//导出Excel
			//var_dump("line 320");exit;
			$xlsName  = "用户数据表";
			$xlsCell  = array(
					array('index','序号'),
					array('username','用户姓名'),
					array('userphone','用户电话'),
					array('is_exchange','是否兑换'),
					array('regtime','注册时间'),
		
			);
			
			 
			 $xlsModel = new Model();
			
			$where=" where 1=1";
			
			
			if($_GET['username']!='blank'){
				$where.=" and username like '%".$_GET['username']."%'";
			} 
				
			if($_GET['userphone']!='blank'){
				$where.=" and userphone like '%".$_GET['userphone']."%'";
			}
			
			if($_GET['isexchange'] != 'blank'){
				$where.= " and is_exchange = ".$_GET['isexchange'];
			}
						
			$where.=" order by id desc";
			$queryStr1 ="select username,userphone,is_exchange,regtime from tp_user".$where;
			
			$xlsData = $xlsModel->query($queryStr1);
			//var_dump($queryStr1);exit;
			$i=1;
			foreach ($xlsData as $k => $v)
			{	
				$xlsData[$k]['index']=$i;
				$i++;
				$xlsData[$k]['userphone']=' '.$xlsData[$k]['userphone'];
				if($xlsData[$k]['is_exchange'] == 1){
					$xlsData[$k]['is_exchange'] = '已兑换';
				}else{
					$xlsData[$k]['is_exchange'] = '未兑换';
				}
			}
			
			$this->exportExcel($xlsName,$xlsCell,$xlsData);
			
			
		
		}
		
	
	
		
		public function modifyScore(){
			$id = $_GET['id'];//分数ID
			$score = $_GET['score'];
			$username = $_GET['username'];

			$this->assign('username',$username);
			$this->assign('score',$score);
			$this->assign('id',$id);
			$this->display();
		}
		
		public function doScoreModify(){
			$id= $_POST['id'];
			$score = $_POST['score'];
			
			$data['id']=$_POST['id'];
			$data['score']=$_POST['score'];
			
			$m=M('Score');
			
			$count=$m->save($data);
			
			if($count>0){
				$this->success('修改分数成功','scoreInfo');
			}else{
				$this->error('修改分数失败');
			}
			
		}
		
		
		public function exchangeGift(){
			$uid = $_GET['uid'];
			$User = M('User');
			$username = $User->where('id='.$uid)->getField('username');

			$this->assign('exchange_user_name',$username);
			$this->assign('exchange_user_id',$uid);
			$this->display();
		}
		
		public function doExchange(){
			$Exchange=M('Exchange');
			
			$Exchange->create();
			
			$Exchange->operationadmin=$_SESSION['username'];
			$Exchange->exchangedatetime=date("Y-m-d H:i:s");
			$lastId=$Exchange->add();
			
			if($lastId){
				//更新
				$User = M("User"); // 实例化User对象
				// 更改用户的兑换状态值
				$User-> where('id='.$_POST['uid'])->setField('is_exchange',1);
				
				$this->success('兑换操作成功','exchangeInfo');
			}else{
				$this->error('兑换操作失败');
			}
		}
		
		
		public function exchangeInfo(){
				
			$queryStr ="select u.username as username,u.userphone as userphone,e.exchangedatetime as exchangedatetime,e.exchangenote as exchangenote,e.operationadmin as operationadmin ,s.score as score from tp_user u join tp_exchange e on u.id=e.uid join tp_score s on u.id=s.uid ";
			
			$Model = new Model(); // 实例化一个model对象 没有对应任何数据表
			$queryResult = $Model->query($queryStr);
			
			if($queryResult!=null){
				import('ORG.Util.Page');// 导入分页类
				$count      = count($queryResult);// 查询满足要求的总记录数
				$Page       = new Page($count,C('EXCHANGE_PAGE_COUNT'));// 实例化分页类 传入总记录数和每页显示的记录数
				$show       = $Page->show();// 分页显示输出
				// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
				$queryStr1 ="select u.username as username,u.userphone as userphone,e.exchangedatetime as exchangedatetime ,e.exchangenote as exchangenote,e.operationadmin as operationadmin,s.score as score from tp_user u join tp_exchange e on u.id=e.uid join tp_score s on u.id=s.uid order by e.exchangedatetime desc limit ".$Page->firstRow.",".$Page->listRows;
				
				$list = $Model->query($queryStr1);
				$this->assign('data',$list);// 赋值数据集
				$this->assign('page',$show);// 赋值分页输出
						
			}
			
			unset($queryResult);
			$this->display();
			
		}
		
		public function searchExchange(){
			$where=" where 1=1";
			
			if(!empty($_GET['startdate'])){
					
				$where.=" and DATE_FORMAT( e.exchangedatetime,  '%Y-%m-%d' ) >='".date("Y-m-d",strtotime($_GET['startdate']))."'";
			}
			
			if(!empty($_GET['enddate'])){
				$where.=" and DATE_FORMAT( e.exchangedatetime,  '%Y-%m-%d' ) <='".date("Y-m-d",strtotime($_GET['enddate']))."'";
			}
				
			if(!empty($_GET['username'])){
				$where.=" and u.username like '%".$_GET['username']."%'";
			}
				
			if(!empty($_GET['userphone'])){
				$where.=" and u.userphone like '%".$_GET['userphone']."%'";
			}
				
			$queryStr ="select u.username as username,u.userphone as userphone,e.exchangedatetime as exchangedatetime ,e.exchangenote as exchangenote,e.operationadmin as operationadmin, s.score as score from tp_user u join tp_exchange e on u.id=e.uid join tp_score s on u.id=s.uid ".$where;
				
			
			$Model = new Model(); // 实例化一个model对象 没有对应任何数据表
				
			$queryResult = $Model->query($queryStr);
				
				
			if($queryResult!=null){
			
				//带入搜索参数
				$parameter = 'userphone='.urlencode($_GET['userphone']).'&startdate='.urlencode($_GET['startdate']).'&enddate='.urlencode($_GET['enddate']).'&username='.urlencode($_GET['username']).'&userphone='.urlencode($_GET['userphone']);
			
				import('ORG.Util.Page');// 导入分页类
				$count      = count($queryResult);// 查询满足要求的总记录数
				$Page       = new Page($count,C('EXCHANGE_PAGE_COUNT'),$parameter);// 实例化分页类 传入总记录数和每页显示的记录数
				$show       = $Page->show();// 分页显示输出
				// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
				$where.=" order by e.exchangedatetime desc limit ".$Page->firstRow.",".$Page->listRows;
				$queryStr1 ="select u.username as username,u.userphone as userphone,e.exchangedatetime as exchangedatetime ,e.exchangenote as exchangenote,e.operationadmin as operationadmin, s.score as score from tp_user u join tp_exchange e on u.id=e.uid join tp_score s on u.id=s.uid".$where;
				
				$list = $Model->query($queryStr1);
				$this->assign('data',$list);// 赋值数据集
				$this->assign('page',$show);// 赋值分页输出
			
			}
				
			unset($queryResult);
			$this->display('exchangeInfo'); // 输出模板
				
		}
		
		/**
		 *
		 * 导出 Exchange Excel
		 */
		function exportExchangeData(){//导出Excel
		
			$xlsName  = "兑换数据表";
			$xlsCell  = array(
					array('index','序号')	,
					array('username','用户名称'),
					array('userphone','用户手机'),
					array('score','用户成绩'),
					array('exchangenote','兑换备注'),
					array('exchangedatetime','兑换时间'),
		
			);
			
			$xlsModel = new Model();
			
			$where=" where 1=1";
			
			if($_GET['startdate']!='blank'){
					
				$where.=" and DATE_FORMAT( e.exchangedatetime,  '%Y-%m-%d' ) >='".date("Y-m-d",strtotime($_GET['startdate']))."'";
			}
			
			if($_GET['enddate']!='blank'){
				$where.=" and DATE_FORMAT( e.exchangedatetime,  '%Y-%m-%d' ) <='".date("Y-m-d",strtotime($_GET['enddate']))."'";
			}
				
			if($_GET['username']!='blank'){
				$where.=" and u.username like '%".$_GET['username']."%'";
			}
				
			if($_GET['userphone']!='blank'){
				$where.=" and u.userphone like '%".$_GET['userphone']."%'";
			}
			
			$where.=" order by e.exchangedatetime desc";
			$queryStr1 ="select u.username as username,u.userphone as userphone,e.exchangedatetime as exchangedatetime,e.exchangenote as exchangenote,s.score as score from tp_user u join tp_exchange e on u.id=e.uid join tp_score s on u.id=s.uid".$where;
			
			$xlsData = $xlsModel->query($queryStr1);
			
			
			$i=1;
			foreach ($xlsData as $k => $v)
			{
				$xlsData[$k]['index']=$i;
				$i++;
			}
				
			$this->exportExcel($xlsName,$xlsCell,$xlsData);
		
		}
	}
?>
